package org.tsp.android.CameraExample;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback, PictureCallback, LocationListener {
	
	 private SurfaceHolder holder;
     private Camera camera;
     private String mFileName;

	public CameraSurfaceView(Context context) {
		super(context);
		
		//Initiate the Surface Holder properly
        this.holder = this.getHolder();
        this.holder.addCallback(this);
        this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(width, height);
        camera.setParameters(parameters);
        camera.startPreview();
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try
        {
            //Open the Camera in preview mode
            this.camera = Camera.open();
            this.camera.setPreviewDisplay(this.holder);
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace(System.out);
        }
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// Surface will be destroyed when replaced with a new screen
        //Always make sure to release the Camera instance
        camera.stopPreview();
        camera.release();
        camera = null;
	}
	
	public void takePhoto(String filename)
	{
		mFileName = filename;
		camera.takePicture(null, null, this);
	}

	@Override
	public void onPictureTaken(byte[] data, Camera camera) {
		// To complete
		// Take picture
		camera.startPreview();
	}

	@Override
	public void onLocationChanged(Location location) {
		// To complete
		// Set gps data to camera parameters 
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
